from django.db import models
from utils.baseModel import UtilsModel
from apps.config.models import Dept_model
from apps.users.models import Users_model
# Create your models here.
# 客户类
class Customers_poll_model(UtilsModel):
    cus_code = models.CharField(max_length=18, null=False,  unique=True, verbose_name='被审单位代码')
    cus_audited_object = models.CharField(max_length=128, null=False,  verbose_name='被审计单位')
    entrust_type = (
        ('01', '自委托模式'),
        ('02', '第三方委托'),
        ('03', '同行合作'),
    )
    cus_marital = models.CharField(choices=entrust_type, max_length=16,  null=False, verbose_name='委托模式')
    cus_phone = models.CharField(max_length=11,default='', null=False,  verbose_name='电话')
    cus_entrust_code = models.CharField(max_length=18, null=False,  default="", verbose_name='委托方代码')
    cus_entrust_name = models.CharField(max_length=128, null=False,  verbose_name='委托方名称')
    cus_cooperation_code = models.CharField(max_length=18, null=False,  default="", verbose_name='合作方代码')
    cus_cooperation_name = models.CharField(max_length=128, null=False,  verbose_name='合作单位')
    cus_address = models.CharField(max_length=128,null=False, default="",verbose_name='客户地址')
    cus_created_person = models.ForeignKey(Users_model, on_delete=models.CASCADE, verbose_name='创建人')
    class Meta:
        verbose_name_plural = '客户表'

    def __str__(self):
        return self.cus_code

# 业务类型编码
class Type_coderule_model(UtilsModel):
    rule_perfix = models.CharField(max_length=16,  null=False, verbose_name='前缀')
    rule_year = models.CharField(max_length=4,null=False, verbose_name='年份')
    rule_suffix = models.PositiveIntegerField(null=False,verbose_name='生成位数')
    rule_begin = models.PositiveIntegerField(null=False,default=1,verbose_name='起始参数')
    rule_nownum = models.PositiveIntegerField(null=False,default=0,verbose_name='业务笔数')
    type_rule = models.ForeignKey('Business_type_model', on_delete=models.CASCADE, default='', verbose_name='对应业务类型编码')
    class Meta:
        verbose_name_plural = '业务类型编码表'

    def __str__(self):
        return self.rule_perfix

# 业务类型
class Business_type_model(UtilsModel):
    type_code = models.CharField(max_length=24,  null=False, unique=True, verbose_name='业务编码')
    type_name = models.CharField(max_length=255,  null=False, verbose_name='业务名称')
    type_business = models.ForeignKey('self', null=True,  on_delete=models.CASCADE, verbose_name='业务隶属')
    def __str__(self):
        return self.type_code

    class Meta:
        verbose_name_plural = '业务类型表'


# 约定书表
class Contract_papers_model(models.Model):
    paper_code = models.CharField(max_length=64, null=False,  unique=True, verbose_name='约定书唯一编码')
    paper_date = models.DateField(null=False, verbose_name='签订日期')
    paper_customer = models.ForeignKey(Customers_poll_model, on_delete=models.CASCADE,verbose_name='关联客户')
    paper_create = models.ForeignKey(Users_model, related_name='creater',on_delete=models.CASCADE, verbose_name='起草人')
    paper_manager = models.ForeignKey(Users_model, related_name='manager', on_delete=models.CASCADE, verbose_name='负责人')
    paper_type = models.ForeignKey(Business_type_model, related_name='papertype', on_delete=models.CASCADE, verbose_name='业务类型')
    paper_company = models.ForeignKey(Dept_model, related_name='papercom',on_delete=models.CASCADE,verbose_name='合同主体')
    paper_money = models.PositiveIntegerField(null=False, default=0,verbose_name='项目金额')
    paper_dept = models.ManyToManyField(Dept_model, verbose_name='承做部门')
    paper_file = models.ManyToManyField('Paper_files_model',verbose_name='附件')
    is_issue = (
        (0, '出具'),
        (1, '不出具'),
    )
    whether_issue = models.CharField(choices=is_issue, max_length=16,  null=False, default=0,
                                    verbose_name='是否出具报告')
    status_type = (
        ('01', '拟定'),
        ('02', '流失'),
        ('03', '执行中'),
        ('04', '履约完成')
    )
    paper_status = models.CharField(choices=status_type, max_length=16,  null=False,default='01', verbose_name='合约状态')
    remark_describe = models.TextField(verbose_name='描述',  null=True)
    create_time = models.DateTimeField(auto_now=True,verbose_name='最后一次修改时间')
    first_time = models.DateTimeField(auto_now_add=True,verbose_name='首次创建时间')
    def __str__(self):
        return self.paper_code

    class Meta:
        verbose_name_plural = '约定书表'

# 合同附件表
class  Paper_files_model(UtilsModel):
    file_url = models.FileField(upload_to='papers/',verbose_name='附件路径')
    def __str__(self):
        return self.file_url.url

    class Meta:
        verbose_name_plural = '附件表'

# 客户申请修改表
class Customers_edit_model(models.Model):
    first_time = models.DateTimeField(auto_now_add=True, verbose_name='首次创建时间')
    touser_remark = models.TextField(verbose_name='对外描述',  null=True)
    remark_describe = models.TextField(verbose_name='描述',  null=True)
    # 需要更改的客户信息id
    log_tocus = models.ForeignKey(Customers_poll_model,null=False,on_delete=models.CASCADE,verbose_name='更改的用户对象')
    # 操作人
    from_person = models.ForeignKey(Users_model,null=False,related_name='from_person',on_delete=models.CASCADE,verbose_name='发起人')
    # 对接人
    to_person = models.ForeignKey(Users_model, null=False,related_name='to_person', on_delete=models.CASCADE, verbose_name='对接人 ')
    # 是否已经处理
    choices = (
        (0, '已处理'), (1, '未处理')
    )
    is_done = models.IntegerField(verbose_name='是否已处理', choices=choices, default=1,  null=False)
    class Meta:
        verbose_name_plural = '客户表操作日志表'